
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>React.Component · React 基础学习资料</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="小翼">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter03/00.html" />
    
    
    <link rel="prev" href="04.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    React 手稿
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter01/00.html">
            
                <a href="../chapter01/00.html">
            
                    
                    基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter01/01.html">
            
                <a href="../chapter01/01.html">
            
                    
                    初识React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter01/02.html">
            
                <a href="../chapter01/02.html">
            
                    
                    React 特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter01/03.html">
            
                <a href="../chapter01/03.html">
            
                    
                    React Initial Project
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter01/04.html">
            
                <a href="../chapter01/04.html">
            
                    
                    Component和Dom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../chapter01/05.html">
            
                <a href="../chapter01/05.html">
            
                    
                    生态组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="00.html">
            
                <a href="00.html">
            
                    
                    组件生命周期
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="01.html">
            
                <a href="01.html">
            
                    
                    装载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="02.html">
            
                <a href="02.html">
            
                    
                    更新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="03.html">
            
                <a href="03.html">
            
                    
                    卸载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="04.html">
            
                <a href="04.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.5" data-path="05.html">
            
                <a href="05.html">
            
                    
                    React.Component
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter03/00.html">
            
                <a href="../chapter03/00.html">
            
                    
                    state
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter03/01.html">
            
                <a href="../chapter03/01.html">
            
                    
                    Component state
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter03/02.html">
            
                <a href="../chapter03/02.html">
            
                    
                    React-Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter03/03.html">
            
                <a href="../chapter03/03.html">
            
                    
                    React-Router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter03/04.html">
            
                <a href="../chapter03/04.html">
            
                    
                    redux-saga
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter04/00.html">
            
                <a href="../chapter04/00.html">
            
                    
                    组件其他方法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter04/01.html">
            
                <a href="../chapter04/01.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter04/02.html">
            
                <a href="../chapter04/02.html">
            
                    
                    类属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter04/03.html">
            
                <a href="../chapter04/03.html">
            
                    
                    实例属性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter05/00.html">
            
                <a href="../chapter05/00.html">
            
                    
                    React 高级用法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter05/01.html">
            
                <a href="../chapter05/01.html">
            
                    
                    keys
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter05/02.html">
            
                <a href="../chapter05/02.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter05/03.html">
            
                <a href="../chapter05/03.html">
            
                    
                    Refs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter05/04.html">
            
                <a href="../chapter05/04.html">
            
                    
                    Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter05/05.html">
            
                <a href="../chapter05/05.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter05/06.html">
            
                <a href="../chapter05/06.html">
            
                    
                    高阶组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter05/07.html">
            
                <a href="../chapter05/07.html">
            
                    
                    类型查检
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter05/08.html">
            
                <a href="../chapter05/08.html">
            
                    
                    代码分隔
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter06/00.html">
            
                <a href="../chapter06/00.html">
            
                    
                    React 实例
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter06/01.html">
            
                <a href="../chapter06/01.html">
            
                    
                    List
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter06/02.html">
            
                <a href="../chapter06/02.html">
            
                    
                    Form
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter06/03.html">
            
                <a href="../chapter06/03.html">
            
                    
                    Form高级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter06/04.html">
            
                <a href="../chapter06/04.html">
            
                    
                    Tooltip
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter07/00.html">
            
                <a href="../chapter07/00.html">
            
                    
                    React项目打包部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter07/01.html">
            
                <a href="../chapter07/01.html">
            
                    
                    与服务端交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter07/02.html">
            
                <a href="../chapter07/02.html">
            
                    
                    打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter07/03.html">
            
                <a href="../chapter07/03.html">
            
                    
                    部署
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter08/00.html">
            
                <a href="../chapter08/00.html">
            
                    
                    浅谈ES
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter08/01.html">
            
                <a href="../chapter08/01.html">
            
                    
                    ES6 (ES2015)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter08/02.html">
            
                <a href="../chapter08/02.html">
            
                    
                    ES7 (ES2016)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter08/03.html">
            
                <a href="../chapter08/03.html">
            
                    
                    ES8 (ES2017)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter08/04.html">
            
                <a href="../chapter08/04.html">
            
                    
                    ES9 (ES2018)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter09/00.html">
            
                <a href="../chapter09/00.html">
            
                    
                    React Hooks
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter09/01.html">
            
                <a href="../chapter09/01.html">
            
                    
                    State Hooks useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter09/02.html">
            
                <a href="../chapter09/02.html">
            
                    
                    State Hooks useReducer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >React.Component</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="react-components">React Components</h1>
<h2 id="reactcomponent">React.Component</h2>
<p>  <code>React.Component</code>&#x662F;&#x57FA;&#x4E8E;ES6 <code>class</code>&#x7684;React&#x7EC4;&#x4EF6;&#x3002;</p>
<p>  React&#x5141;&#x8BB8;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;<code>class</code>&#x6216;&#x8005;<code>function</code>&#x4F5C;&#x4E3A;&#x7EC4;&#x4EF6;&#xFF0C;&#x90A3;&#x4E48;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x7C7B;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x7EE7;&#x627F;<code>React.Component</code>.</p>
<p>  &#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx">  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Welcome</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    render() {
      <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {this.props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
    }
  }
</code></pre>
<p>  &#x6CE8;&#x610F;&#xFF1A;&#x7EE7;&#x627F;<code>React.Component</code>&#x7684;React&#x7EC4;&#x4EF6;&#x7C7B;&#x4E2D;&#xFF0C;<code>render()</code>&#x4E3A;&#x5FC5;&#x987B;&#x65B9;&#x6CD5;&#xFF0C;&#x5176;&#x4ED6;&#x90FD;&#x4E3A;&#x53EF;&#x9009;&#x3002;</p>
<h2 id="reactpurecomponent">React.PureComponent</h2>
<p>  <code>React.PureComponent</code> &#x548C; <code>React.Component</code>&#x7C7B;&#x4F3C;&#xFF0C;&#x90FD;&#x662F;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x7C7B;&#x3002;&#x4E0D;&#x540C;&#x662F;<code>React.Component</code>&#x6CA1;&#x6709;&#x5B9E;&#x73B0;<code>shouldComponentUpdate()</code>&#xFF0C;&#x800C; <code>React.PureComponent</code>&#x901A;&#x8FC7;<code>props</code>&#x548C;<code>state</code>&#x7684;&#x6D45;&#x6BD4;&#x8F83;&#x5B9E;&#x73B0;&#x4E86;&#x3002;</p>
<p>  &#x5982;&#x679C;&#x7EC4;&#x4EF6;&#x7684;<code>props</code>&#x548C;<code>state</code>&#x76F8;&#x540C;&#x65F6;&#xFF0C;<code>render</code>&#x7684;&#x5185;&#x5BB9;&#x4E5F;&#x4E00;&#x81F4;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>React.PureComponent</code>&#x4E86;,&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x63D0;&#x9AD8;&#x7EC4;&#x4EF6;&#x7684;&#x6027;&#x80FD;&#x3002;</p>
<p>  &#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx">  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Welcome</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">PureComponent</span> </span>{
    render() {
      <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {this.props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
    }
  }
</code></pre>
<blockquote>
<p>&#x5F53;props&#x548C;state&#x4E2D;&#x6709;&#x590D;&#x6742;&#x6570;&#x636E;&#x7ED3;&#x679C;&#x65F6;&#xFF0C;&#x4E0D;&#x597D;&#x4F7F;&#x7528;<code>PureComponent</code>&#x3002;</p>
</blockquote>
<h2 id="reactmemo">React.memo</h2>
<p>  <code>React.memo</code>&#x662F;&#x4E00;&#x4E2A;&#x9AD8;&#x9636;&#x7EC4;&#x4EF6;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;<code>React.PureComponent</code>&#xFF0C;&#x4E0D;&#x540C;&#x4E8E;<code>React.memo</code>&#x662F;<code>function</code>&#x7EC4;&#x4EF6;&#xFF0C;<code>React.PureComponent</code>&#x662F;<code>class</code>&#x7EC4;&#x4EF6;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-jsx">  <span class="hljs-keyword">const</span> MyComponent = React.memo(props =&gt; {
    <span class="hljs-comment">/* render using props */</span>
    <span class="hljs-keyword">return</span> (

    );
  });
</code></pre>
<p>  &#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x4F9D;&#x7136;&#x662F;&#x4E00;&#x79CD;&#x5BF9;&#x8C61;&#x7684;&#x6D45;&#x6BD4;&#x8F83;&#xFF0C;&#x6709;&#x590D;&#x6742;&#x5BF9;&#x8C61;&#x65F6;&#x65E0;&#x6CD5;<code>render</code>&#x3002;&#x5728;<code>React.memo</code>&#x4E2D;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49;&#x5176;&#x6BD4;&#x8F83;&#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x3002;</p>
<p>  &#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params">props</span>) </span>{
    <span class="hljs-comment">/* render using props */</span>
  }
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">areEqual</span>(<span class="hljs-params">prevProps, nextProps</span>) </span>{
    <span class="hljs-comment">/*
    return true if passing nextProps to render would return
    the same result as passing prevProps to render,
    otherwise return false
    */</span>
  }
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> React.memo(MyComponent, areEqual);
</code></pre>
<blockquote>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x5728;V16.6.0&#x624D;&#x652F;&#x6301;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="04.html" class="navigation navigation-prev " aria-label="Previous page: 异常处理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter03/00.html" class="navigation navigation-next " aria-label="Next page: state">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"React.Component","level":"1.3.5","depth":2,"next":{"title":"state","level":"1.4","depth":1,"path":"chapter03/00.md","ref":"./chapter03/00.md","articles":[{"title":"Component state","level":"1.4.1","depth":2,"path":"chapter03/01.md","ref":"./chapter03/01.md","articles":[]},{"title":"React-Redux","level":"1.4.2","depth":2,"path":"chapter03/02.md","ref":"./chapter03/02.md","articles":[]},{"title":"React-Router","level":"1.4.3","depth":2,"path":"chapter03/03.md","ref":"./chapter03/03.md","articles":[]},{"title":"redux-saga","level":"1.4.4","depth":2,"path":"chapter03/04.md","ref":"./chapter03/04.md","articles":[]}]},"previous":{"title":"异常处理","level":"1.3.4","depth":2,"path":"chapter02/04.md","ref":"./chapter02/04.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"root":"./books","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"小翼","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React 基础学习资料","language":"zh-hans","links":{"gitbook":false,"sharing":{"google":true,"facebook":true,"twitter":true}},"gitbook":"*"},"file":{"path":"chapter02/05.md","mtime":"2020-03-07T07:33:43.853Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-09T08:08:41.080Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

